﻿using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PdfOperator
{
    public partial class FrmMerge : Form
    {
        public FrmMerge()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择要合并的PDF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFiles = new OpenFileDialog();
            openFiles.Multiselect = true;
            openFiles.Filter = "pdf文件|*.pdf";
            if (openFiles.ShowDialog() == DialogResult.OK)
            {
                string[] filenames = openFiles.FileNames;
                Array.Sort(filenames);
                foreach (string filename in filenames)
                {
                    lstBoxFileList.Items.Add(filename);
                }
            }

        }

        /// <summary>
        /// 选择保存路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSavePath_Click(object sender, EventArgs e)
        {
            if (lstBoxFileList.Items.Count < 1)
            {
                MessageBox.Show("请添加要合并的pdf文件！");
                return;
            }
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Title = "选择保存文件路径";
            saveFile.Filter = "pdf文件|.*pdf";
            saveFile.DefaultExt = ".pdf";
            saveFile.FileName = "合并的PDF1.pdf";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = saveFile.FileName;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (lstBoxFileList.Items.Count < 1)
            {
                MessageBox.Show("请添加要合并的PDF文件！");
                return;
            }
            if (string.IsNullOrEmpty(txtSavePath.Text))
            {
                MessageBox.Show("请选择合并的PDF文件保存路径！");
                return;
            }

            List<string> filesList = new List<string>();
            for (int i = 0; i < lstBoxFileList.Items.Count; i++)
            {
                string a = lstBoxFileList.Items[i].ToString();
                //Console.WriteLine(a);
                filesList.Add(a);
            }
            string[] files = filesList.ToArray();

            PdfDocumentBase doc = PdfDocument.MergeFiles(files);
            doc.Save(txtSavePath.Text, FileFormat.PDF);
            MessageBox.Show("完成");
        }

        private void btnOpenMergePdf_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!System.IO.File.Exists(txtSavePath.Text))
            {
                return;
            }
            else
            {
                //存在文件
                System.Diagnostics.Process.Start(txtSavePath.Text);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
